package com.netty.demo.server.netty.enums;

/**
 * 这里面定义不同的协议头
 *
 * @author 余秀良
 * @version 1.0
 * <p>date: 2019年09月27日  14:42 </p>
 * <p>address: 四川成都 </p>
 */
public enum RequestHeaderProtocol {
    /**
     * A4-08协议
     */
    A408("A4-08", 0xA4, 0x08),
    /**
     * A4-09协议
     */
    A409("A4-09", 0xA4, 0x09),
    ;
    private final String value;
    private final int messageType;
    private final int messageId;

    RequestHeaderProtocol(String value, int messageType, int messageId) {
        this.value = value;
        this.messageType = messageType;
        this.messageId = messageId;
    }

    public static RequestHeaderProtocol value(String value) {
        return forValue(value);
    }

    public static RequestHeaderProtocol value(String value, int messageType, int messageId) {
        for (RequestHeaderProtocol protocol : RequestHeaderProtocol.values()) {
            if (protocol.getValue().equals(value) && Integer.toUnsignedLong(protocol.getMessageType()) == Integer.toUnsignedLong(messageType) && Integer.toUnsignedLong(protocol.getMessageId()) == Integer.toUnsignedLong(messageId)) {
                return protocol;
            }
        }
        return null;
    }

    public static RequestHeaderProtocol value(int messageType, int messageId) {
        for (RequestHeaderProtocol protocol : RequestHeaderProtocol.values()) {
            if (Integer.toUnsignedLong(protocol.getMessageType()) == Integer.toUnsignedLong(messageType) && Integer.toUnsignedLong(protocol.getMessageId()) == Integer.toUnsignedLong(messageId)) {
                return protocol;
            }
        }
        return null;
    }

    private static RequestHeaderProtocol forValue(String value) {
        for (RequestHeaderProtocol protocol : RequestHeaderProtocol.values()) {
            if (protocol.getValue().equals(value)) {
                return protocol;
            }
        }
        return null;
    }

    public final String getValue() {
        return value;
    }

    public final int getMessageType() {
        return messageType;
    }

    public final int getMessageId() {
        return messageId;
    }
}
